﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace HOOMT
{
    class AttributeDBO:SqliteDataBaseOperator
    {
        public bool DeleteAttribute(Attribute attr)
        {
            try
            {
              
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from Attribute where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));

                cmd.ExecuteNonQuery();


                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        public bool AddAttribute(Attribute attr, String parentId)
        {
            try
            {
                this.DeleteAttribute(attr);
                //   this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into Attribute( id ,parentId,accessModifier,attributeType,attributeName,initialValue,description)  "
                    + "values ( @id ,@parentId,@accessModifier,@attributeType,@attributeName,@initialValue,@description)";
                cmd.Parameters.Add(new SQLiteParameter("id", attr.id));
                cmd.Parameters.Add(new SQLiteParameter("parentId", parentId));
                cmd.Parameters.Add(new SQLiteParameter("accessModifier", attr.AccessModifier));
                cmd.Parameters.Add(new SQLiteParameter("attributeType", attr.AttributeType));
                cmd.Parameters.Add(new SQLiteParameter("attributeName", attr.AttributeName));
                cmd.Parameters.Add(new SQLiteParameter("initialValue", attr.InitialValue));
                cmd.Parameters.Add(new SQLiteParameter("description", attr.Description));
                cmd.ExecuteNonQuery();
                
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }

        }

        public List<Attribute> GetAttributeList(ObjectDiagram diagram)
        {
            try
            {
                List<Attribute> list = new List<Attribute>();


                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from Attribute where parentid=@parentid";
                cmd.Parameters.Add(new SQLiteParameter("parentid", diagram.Id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Attribute attr = new Attribute(reader["accessModifier"].ToString(),
                        reader["attributeType"].ToString(),
                        reader["attributeName"].ToString(),
                        reader["initialValue"].ToString(),
                        reader["id"].ToString(),
                        reader["description"].ToString());
                    list.Add(attr);
                }

                return list;

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                this.CloseConnection();
            }
        }


    }
}
